package com.zyx.springmvc.reqparam.controller;

import com.zyx.springmvc.reqparam.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/14 09:33
 */
@Controller
public class ParamController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/reqparam")
    public String reqparam() {
        return "reqparam";
    }

    @RequestMapping("/testServletAPI")
    public String testParam(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println(session);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:" + username + ",password:" + password);
        return "success";
    }

    @RequestMapping("/testParam")
    public String testParam(@RequestParam(name = "user_name", defaultValue = "hehe") String username,
                            String password,
                            String[] hobby,
                            @RequestHeader(name = "host") String host,
                            @CookieValue("JSESSIONID") String jSessionid) {
        System.out.println("username: " + username + ",password: " + password);
        System.out.println("hobby: " + Arrays.toString(hobby));
        System.out.println("host: " + host);
        System.out.println("JSESSIONID: " + jSessionid);
        return "success";
    }

    @RequestMapping("/testPojo")
    public String testPojo(User user) {
        System.out.println(user);
        return "success";
    }

}
